package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.DishDao;
import com.itheima.dto.DishDto;
import com.itheima.entity.Dish;
import com.itheima.entity.DishFlavor;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王磊
 * @date 2022/5/9
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //现将菜品信息保存到菜品表
        this.save(dishDto);
        //获取菜品的id
        Long dishId = dishDto.getId();
        //获取所有的口味信息
        List<DishFlavor> flavorsList = dishDto.getFlavors();
        //循环将每个口味对应的菜品id赋上值
        flavorsList = flavorsList.stream().map((itm) -> {
                    itm.setDishId(dishId);
                    return itm;
                }
        ).collect(Collectors.toList());
        //将口味保存到口味表
        dishFlavorService.saveBatch(flavorsList);
    }
}
